package cn.liuweiwei.service.impl;

import cn.liuweiwei.domain.Blog;
import cn.liuweiwei.domain.BlogAndTag;
import cn.liuweiwei.mapper.BlogAndTagMapper;
import cn.liuweiwei.mapper.BlogMapper;
import cn.liuweiwei.mapper.TypeMapper;
import cn.liuweiwei.service.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private BlogAndTagMapper blogAndTagMapper;

    @Override
    public PageInfo<Blog> findBlogsByTitle(String pageNum, String pageSize, String title) {
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Blog> blogList = blogMapper.findBlogsByTitle(title);
        return new PageInfo<>(blogList);
    }

    @Override
    @Transactional
    public void insertOneBlog(Blog blog) {
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blogMapper.insertOne(blog);
        Blog newBlog = blogMapper.findOneBlogIdByTitle(blog.getTitle());
        String[] tagIds = blog.getTagIds();
        for (String tagId : tagIds) {
            blogAndTagMapper.insertOne(new BlogAndTag(null, newBlog.getId().toString(), tagId));
        }
    }

    @Override
    public Blog findOneBlogById(String id) {
        Integer viewCount = blogMapper.selectViewCount(id);
        blogMapper.updateViewCount(++viewCount, id);
        Blog blog = blogMapper.findOneBlogById(id);
        blog.setType(typeMapper.findOneById(blog.getTypeId()));
        List<BlogAndTag> tagsList = blogAndTagMapper.findTagIdsByBlogId(id);
        String[] tags = new String[tagsList.size()];
        for (int i = 0; i < tagsList.size(); i++) {
            tags[i] = tagsList.get(i).getTagId();
        }
        blog.setTagIds(tags);
        return blog;
    }

    @Override
    public PageInfo<Blog> findBlogsNoContent(String pageNum, String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Blog> blogList = blogMapper.findAll();
        for (Blog blog : blogList) {
            blog.setType(typeMapper.findOneById(blog.getTypeId()));
        }
        return new PageInfo<>(blogList);
    }

    @Override
    public PageInfo<Blog> orderBlogsByTime(String page, String limit) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<Blog> blogList = blogMapper.orderBlogsByTime();
        return new PageInfo<>(blogList);
    }

    @Override
    @Transactional
    public void updateBlog(Blog blog) {
        blog.setUpdateTime(new Date());
        String[] tagIds = blog.getTagIds();
        for (String tagId : tagIds) {
            blogAndTagMapper.update(new BlogAndTag(null, blog.getId().toString(), tagId));
        }
        blogMapper.update(blog);
    }

    @Override
    @Transactional
    public void deleteOneBlog(String blogId) {
        blogAndTagMapper.deleteById(blogId);
        blogMapper.deleteById(blogId);
    }
}
